package com.mall.admin.controller.goods;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.Kv;
import com.mall.admin.controller.AdminBaseController;
import com.mall.admin.service.goods.GoodsBrandService;
import com.mall.common.annotation.Inject;
import com.mall.common.constant.CommonEnum.LogType;
import com.mall.common.model.goods.GoodsBrand;
import com.mall.common.service.base.Result;

/**
 * 品牌管理
 * @author taoge
 * @version 1.0
 * @create_at 2017年8月27日
 */
@ControllerBind(controllerKey="/admin/goods/brand")
public class GoodsBrandController extends AdminBaseController{

	@Inject
	private GoodsBrandService goodsBrandService;
	
	public  void index() {
		renderView("/goods/brand/index.vm");
	}
	
    public void list(){
    	int pageNumber = getParaToInt("pageNumber",1);
    	int pageSize = getParaToInt("pageSize",10);
    	String name = getPara("name");
    	Kv condition = Kv.by("name", name);
    	Result result = goodsBrandService.list(pageSize,pageNumber,condition);
		setAttr("page", result.getDefaultModel());
		renderView("/goods/brand/list.vm");
	}
    
    /**
	 * 删除单条类别
	 */
	public void deleteById(){
		int id = getParaToInt("id");
		Result result = goodsBrandService.deleteById(id);
		if(result.isSuccess()){
			systemLog("删除类别", LogType.DELETE.getValue());
		}
		renderJson(result.getResultCode());
	}
	
	/**
	 * 批量删除品牌
	 */
    public void deleteBatch(){
		String ids = getPara("ids");
		Result result = goodsBrandService.deleteBatch(ids);
		renderJson(result.getResultCode());
	}
    
	public void add(){
		renderView("/goods/brand/add.vm");
	}
	
    public void create(){
    	GoodsBrand goodsBrand = getModel(GoodsBrand.class, "goodsBrand");
		Result result = goodsBrandService.create(goodsBrand);
		renderJson(result.getResultCode());
	}

    public void edit(){
    	int brandId = getParaToInt("id");
		setAttr("goodsBrand", GoodsBrand.dao.findById(brandId));
		renderView("/goods/brand/edit.vm");
	}
    
    public void update(){
		int id = getParaToInt(0);
    	String oldName = GoodsBrand.dao.findById(id).getStr("name");
    	GoodsBrand goodsBrand = getModel(GoodsBrand.class,"goodsBrand");
    	Result result = goodsBrandService.update(oldName,goodsBrand);
    	renderJson(result.getResultCode());
	}
}
